home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F24910_replace.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2001-08-08  |  3.3 KB  |  82 lines

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  3. <!--=======================================================================
  4.  
  5.     replace.xsl
  6.  
  7.     This is an example of using a named template to do substring
  8.     replacement. It relies on tail recursion, in order to be XSLT 1.0
  9.     conformant. This template can be used when the XPath translate()
  10.     function, which operates on single characters in a string, is
  11.     insufficient. It uses the XPath functions substring-before(),
  12.     substring-after(), and concat(), and is quite simple.
  13.  
  14.     Each time the named template is invoked, it is given a string to
  15.     perform the replacement on, a substring to find and replace, and a
  16.     replacement string. It produces a result tree fragment containing a
  17.     text node with the first instance of the matched string having been
  18.     replaced.
  19.     
  20.     If there was any text after that first match, the template calls
  21.     itself again, but makes the string to perform the replacement on be
  22.     just the text after the first match. When the end of the string is
  23.     reached, the template won't call itself again.
  24.     
  25.     The series of text nodes that it produces during this process are
  26.     automatically concatenated into one text node, which you can leave
  27.     in the result tree fragment for most situations. If it is
  28.     necessary to have an actual string object, use the string()
  29.     function to use the string-value of the result tree fragment.
  30.  
  31.     Written by: Mike J. Brown <mike@skew.org>
  32.     License: none; use and distribute freely.
  33.  
  34.     Version 1.0 - 10 Nov 2000: First public version. XSLT 1.0 conformant.
  35.  
  36. =======================================================================-->
  37.  
  38.     <!-- arbitrary output method just for demonstration purposes -->
  39.     <xsl:output method="text"/>
  40.  
  41.     <!-- template matching root node of arbitrary source tree -->
  42.     <xsl:template match="/">
  43.         <!-- example showing replacement of '%20' with a single space -->
  44.         <xsl:variable name="myString" select="'This%20is%20a%20Test'"/>
  45.         <xsl:variable name="myNewString">
  46.             <!-- go create the result tree fragment with the replacement -->
  47.             <xsl:call-template name="SubstringReplace">
  48.                 <xsl:with-param name="stringIn" select="$myString"/>
  49.                 <xsl:with-param name="substringIn" select="'%20'"/>
  50.                 <xsl:with-param name="substringOut" select="' '"/>
  51.             </xsl:call-template>
  52.         </xsl:variable>
  53.         <!--
  54.         show the new string. concat() will treat the result tree
  55.         fragment as if it were a string returned by the string() function.
  56.         -->
  57.         <xsl:value-of select="concat(' input: ',$myString,' output: ',$myNewString)"/>
  58.     </xsl:template>
  59.  
  60.     <!-- here is the template that does the replacement -->
  61.     <xsl:template name="SubstringReplace">
  62.         <xsl:param name="stringIn"/>
  63.         <xsl:param name="substringIn"/>
  64.         <xsl:param name="substringOut"/>
  65.         <xsl:choose>
  66.             <xsl:when test="contains($stringIn,$substringIn)">
  67.                 <xsl:value-of select="concat(substring-before($stringIn,$substringIn),$substringOut)"/>
  68.                 <xsl:call-template name="SubstringReplace">
  69.                     <xsl:with-param name="stringIn" select="substring-after($stringIn,$substringIn)"/>
  70.                     <xsl:with-param name="substringIn" select="$substringIn"/>
  71.                     <xsl:with-param name="substringOut" select="$substringOut"/>
  72.                 </xsl:call-template>
  73.             </xsl:when>
  74.             <xsl:otherwise>
  75.                 <xsl:value-of select="$stringIn"/>
  76.             </xsl:otherwise>
  77.         </xsl:choose>
  78.     </xsl:template>
  79.  
  80. </xsl:stylesheet>
  81.  
  82.